package cn.xie.blog.commonutil.util;

import lombok.experimental.UtilityClass;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


/**
 * @author: xiaoxie
 * create: 2025-04-21 21:56
 * @BelongsProject: blog-parent
 * @BelongsPackage: cn.xie.blog.commonutil.util
 * description: MD5工具类
 */
@UtilityClass
public class MD5Util {
    public static String encrypt(String strSrc) {
        if (strSrc == null || strSrc.isEmpty()) {
            return null;
        }
        char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            // 获取MD5摘要算法的实例
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 使用指定的字节数组更新摘要
            md.update(strSrc.getBytes());
            // 获取密文
            byte[] bytes = md.digest();
            // 将字节数组转换为十六进制字符串
            int j = bytes.length;
            char[] chars = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte b = bytes[i];
                chars[k++] = hexDigits[b >>> 4 & 0xf];
                chars[k++] = hexDigits[b & 0xf];
            }
            return new String(chars);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        System.out.println(MD5Util.encrypt("111111"));
    }

}
